{    
Indicator:          _SharpeRatio.RS

Description:        Simplified indicator to Calculate a single SR value, for use with scanner.

                    Divides a time period into segments, and calculates price gain in percent for each segment.
                    If logarithmic growth is occuring, each segment will have a similar percentage gain.

                    Radarscreen: LOAD ADDITION DATA MUST BE SET TO AT LEAST = [Offset + SegLength*MaxList(Segments1,Segments2) + 3]

Note:               When RiskFreeReturn is > 0, the graph of sharpe ratio using this formula may appear to exhibit some instability,
                    abruptly changing values from plus to minus values.  This is because of the modification of the formula
                    where the sign of the RiskFreeReturn flips from positive to negative according to whether the 
                    AvgRR (Average rate of return) is a positive number or a negative.  This modification allows the forumla
                    to be used to screen for both potential longs and potential shorts, correctly adjusting the sign of the
                    RiskFreeReturn accordingly.

                    However, when using this calculation as a measure of performance, rather than for comparing potential 
                    long investments to potential short investments, it will be more useful to assume a RiskFreeReturn = 0.

                    This will remove any apparent "unstable" behavior in the sharpe ratio signal.

Author:             Mark J. Krisburg 

Last Modified:      12/16/10        original version
                    07/17/11        calculate only when LastBarOnChart is reached.  Calculation on other bars will
                                    Not be of use in Radarscreen indicator.
                    09/20/11        Add Length column before sharpe ratio                                    
} 
  
 
Inputs:        
    Price(avgprice), 
    SegLength(1),                    // use = 1 for weekly or monthly monthly charts, = 5 for daily charts (1 week) or = 22 (1 month)
    SegsPerYear(251),                // = 52 if SegLength = 5 on Day Chart(1 week), = 12 if SegLength = 1 on Monthly Chart 
    Segments(80),    
    Offset(0),
    RiskFreeReturn(0),               // annualized risk free return in percent.
    ShowMultiColor(false);
    
Vars:    
    Intrabarpersist bool LastBar(false),                    
    avgRR(0),                        // average return rate for 'Segments1' segments
    sdRR(0),                         // standard deviation of return rates
    intrabarpersist annRR(0),        // annualized rate of return = RR * 12 (1 segment = 1 month)
    annSD(0),                        // annualized standard deviation of returns = sdRR * 12 (1 segment = 1 month)
    excessRR(0),                     // excess return = annualized rate of return - risk free return
    intrabarpersist SR(0);           // Sharpe Ratio = Excess return / annualized standard deviation of returns
    
            
    Once (_LastBarOnChart) begin
        LastBar = true;
    end;        

If LastBar and BarStatus(1) = 2 then begin        // prevent calculation every tick, even if Update every tick selected by mistake
    value1 = _SharpeRatio(Price, SegLength, SegsPerYear, Segments, RiskFreeReturn, 
                Offset, avgRR, sdRR, annRR, annSD, excessRR, SR);

    Plot1(Segments, "Len");
    Plot2(Offset, "Off");
    Plot3(SR, "SR");
    plot4(AbsValue(SR), "Abs SR");    
 
    If ShowMultiColor then begin
        Switch SR begin
            Case > 2:
                SetPlotBGcolor(3, green);
                SetPlotColor(3, black);
            Case > 1:
                SetPlotBGColor(3, darkgreen);
            Case < -2:
                SetPlotBGColor(3, red);
            Case < -1:
                SetPlotBGColor(3, darkred);                                    
        end;        
    end;
        
end;